//
//  WYDataPicker.m
//  Boss
//
//  Created by admin on 2018/6/27.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "WYDataPicker.h"
@interface WYDataPicker()
@property (nonatomic,strong)UIDatePicker *dataPicker;
@property (nonatomic,strong)UIToolbar *inputAccessoryView;
@property (nonatomic,strong)NSString *date;
@end
@implementation WYDataPicker

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self createSubViews];
    }
    return self;
}

-(void)createSubViews
{
    self.backgroundColor = [UIColor whiteColor];
    self.dataPicker  = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, 44, self.width,self.height-44)];
    [self.dataPicker setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"zh_Hans_CN"]];
    
    [self.dataPicker setCalendar:[NSCalendar currentCalendar]];
    
    //    设置DatePicker的时区。
    
    //    默认为设置为：[datePicker setTimeZone:[NSTimeZone defaultTimeZone]];
    
    //    设置DatePicker的日期。
    
    //    默认设置为:
    
   
    [self.dataPicker setDatePickerMode:UIDatePickerModeDate];
    //    minimumDate设置DatePicker的允许的最小日期。
    
    //    maximumDate设置DatePicker的允许的最大日期
    
    
    NSDate *currentDate = [NSDate date];
    [self.dataPicker setMaximumDate:currentDate];
    NSDateFormatter *format = [[NSDateFormatter alloc]init];
    [format setDateFormat:@"yyyy-MM-dd"];
    NSString *str = @"1900-01-01";
   
    NSDate *maxDate = [format dateFromString:str];
    [self.dataPicker setMinimumDate:maxDate];
//    self.dataPicker setMaximumDate:<#(NSDate * _Nullable)#>
    
    
    NSString *selectDate =@"1990-01-01";
     self.date = selectDate;
    [self.dataPicker setDate:[format dateFromString:selectDate]];
    
   
    
    [self.dataPicker addTarget:self action:@selector(dateChange:)forControlEvents:UIControlEventValueChanged];
    [self addSubview:self.dataPicker];
    
    
    
    
   self.inputAccessoryView= [[UIToolbar alloc]init];
    
     self.inputAccessoryView.barStyle=UIBarStyleDefault;
    
  self.inputAccessoryView.autoresizingMask=UIViewAutoresizingFlexibleHeight;
    
    [ self.inputAccessoryView sizeToFit];
    
    self.inputAccessoryView.frame=CGRectMake(0,0,kScreenWidth,44);
    
//    [UIBarButtonItem alloc] initWithTitle:<#(nullable NSString *)#> style:<#(UIBarButtonItemStyle)#> target:<#(nullable id)#> action:<#(nullable SEL)#>
    
    UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStyleDone target:self action:@selector(cancel:)];
    
    UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStyleDone target:self action:@selector(done:)];
    
    UIBarButtonItem *flexibleSpaceLeft = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    
    NSArray*array = [NSArray arrayWithObjects:cancelBtn,flexibleSpaceLeft, doneBtn,nil];
    
    [ self.inputAccessoryView setItems:array];

    [self addSubview:self.inputAccessoryView];
}
- (void)dateChange:(UIDatePicker *)date
{
    NSDateFormatter *format = [[NSDateFormatter alloc]init];
    if ([self.type isEqualToString:@"11"]) {
        [format setDateFormat:@"yyyy-MM-dd"];
    }else{
        [format setDateFormat:@"yyyy-MM"];
    }
    
    self.date = [format stringFromDate:date.date];
    NSLog(@"%@", date.date);
    
}
- (void)done:(UIBarButtonItem*)sender {
    
    self.hidden = YES;
    
    if (self.complete) {
        self.complete(self.date);
    }
    
    [UIView animateWithDuration:.3 animations:^{
        
//        datePicker.frame=CGRectMake(0,kScreenHeight,kScreenWidth,216);
//
//        inputAccessoryView.frame=CGRectMake(0,kScreenHeight,kScreenWidth,44);
        
    }completion:^(BOOL finished) {
        
        
    }];
    
}

- (void)cancel:(UIBarButtonItem*)sender {
    if (self.cancel) {
        self.cancel(@"");
    }
    
    
    [UIView animateWithDuration:.3 animations:^{
        
//        datePicker.frame=CGRectMake(0,kScreenHeight,kScreenWidth,216);
//
//        inputAccessoryView.frame=CGRectMake(0,kScreenHeight,kScreenWidth,44);
        
    }completion:^(BOOL finished) {
        
    }];
    self.hidden = YES;
}



@end
